home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_2 / regula.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  993 b   |  39 lines  |  [MATF/MATL]

  1. function [c,yc,err,P] = regula(f,a,b,delta,epsilon,max1)
  2. % [c,yc,err] = regula(f,a,b,delta,epsilon,max1)
  3. % [c,yc,err,P] = regula(f,a,b,delta,epsilon,max1)
  4. % The Regula-Falsi method is used to locate a root.
  5. % f is the function, input.
  6. % a is the left endpoint, input.
  7. % b is the right endpoint, input.
  8. % delta is the tolerance for c, input.
  9. % epsilon is the tolerance for yc, input.
  10. % max1 is the maximum number of iterations, input.
  11. % c  is the root, output.
  12. % yc is the function value, output.
  13. % err is the error estimate for c, output.
  14. % P is the is the matrix of endpoint iterations, output.
  15. P = [a b];
  16. ya = feval(f,a);
  17. yb = feval(f,b);
  18. if ya*yb > 0, break, end
  19. for k=1:max1,
  20.   dx = yb*(b - a)/(yb - ya);
  21.   c  = b - dx;
  22.   ac = c - a;
  23.   yc = feval(f,c);
  24.   if  yc == 0,
  25.     break;
  26.   elseif  yb*yc > 0,
  27.     b = c;
  28.     yb = yc;
  29.   else
  30.     a = c;
  31.     ya = yc;
  32.   end
  33.   P = [P;a b];
  34.   dx = min(abs(dx),ac);
  35.   if abs(dx) < delta,   break, end
  36.   if abs(yc) < epsilon, break, end
  37. end
  38. err = abs(dx);
  39.